package com.gene.service.impl;

import com.gene.mapper.UpmsUserMapper;
import com.gene.pojo.UpmsUser;
import com.gene.pojo.UpmsUserExample;
import com.gene.pojo.entity.Members;
import com.gene.service.IMembersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 关于二级会员的增删查改
 */
@Service
public class MembersServiceImpl implements IMembersService{
    @Autowired
    private UpmsUserMapper upmsUserMapper;

    /**
     * 根据用户id返回该用户的二级会员
     * @param id
     * @return list
     */
    @Override
    public List<Members> query(Integer id) {
        List<Members> list=upmsUserMapper.queryMembers(id);
        return list;
    }

    /**
     * 根据用户id添加会员
     * @param uid
     * @param sid
     * @return bool
     */
    @Override
    public boolean save(Integer uid, Integer sid) {
        UpmsUserExample example=new UpmsUserExample();
        UpmsUserExample.Criteria criteria=example.createCriteria();
        criteria.andUserIdEqualTo(sid);
        criteria.andSuperiorEqualTo(uid);
        List<UpmsUser> list=upmsUserMapper.selectByExample(example);
        if(list.size()<=0){//不能互相为二级会员
            UpmsUser upmsUser1=new UpmsUser();
            upmsUser1.setSuperior(sid);
            UpmsUserExample example1=new UpmsUserExample();
            UpmsUserExample.Criteria criteria1=example1.createCriteria();
            criteria1.andUserIdEqualTo(uid);
            criteria1.andSuperiorIsNull();
            return upmsUserMapper.updateByExampleSelective(upmsUser1,example1)==1?true:false;
        }
        return false;
    }

}
